<template>
  <!-- 注册页面的整体盒子 -->
  <div class="reg-container">
    <!-- 注册的盒子 -->
    <div class="reg-box">
      <!-- 标题的盒子 -->
      <div class="title-box"></div>
      <!-- 注册的表单区域 -->
      <el-form
        :model="regForm"
        :rules="regRules"
        ref="regForm"
        class="form-phone"
      >
        <el-form-item prop="telephone">
          <el-input
            placeholder="请输入手机号码"
            prefix-icon="el-icon-search"
            v-model="regForm.telephone"
          >
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            v-model="regForm.password"
          >
          </el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input placeholder="请再次输入密码" prefix-icon="el-icon-lock" v-model="regForm.repassword"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width:100%" @click="regist">注册</el-button>
          <el-link type="info">去登录</el-link>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'RegPage',

  data () {
    const checkPwd = (rule, value, callback) => {
      if (value !== this.regForm.password) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    }
    return {
      regForm: {
        telephone: '',
        password: '',
        repassword: ''
      },
      regRules: {
        telephone: [
          // 一个对象是一条规则
          // pattern是正则

          // 正则必须以^开始以$结束,第一位必须以1开始，第二位是3-9之间，第三位必须是0-9，数字，{9}：代表前一位数最多再出现9次
          { required: true, message: '请输入手机号', trigger: 'blur' },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '请输入正确的手机号',
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        repassword: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' },
          { validator: checkPwd, trigger: 'blur' }

        ]

      }
    }
  },
  methods: {
    regist () {
      this.$refs.regForm.validate(async valid => {
        if (!valid) return
        console.log('注册了')
        // 注册流程
        // 1.发送ajax请求
        const { data: res } = await this.$http.post('/api/reg', {
          username: this.regForm.username,
          password: this.regForm.password,
          repassword: this.regForm.repassword
        })
        // 2.获取响应结果
        console.log(res)
        if (res.code !== 0) return this.$message.error(res.message)

        // 3.根据响应的结果提醒用户注册成功或失败
        // 4.跳转至登录页
        // 编程式导航
        this.$message.success('登录成功')
        this.$router.push('/login')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.reg-container {
  background: url('../../assets/images/login_bg.jpg') center;
  background-size: cover;
  height: 100%;

  .reg-box {
    width: 400px;
    height: 335px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    .title-box {
      height: 60px;
      background: url('../../assets/images/login_title.png') center no-repeat;
    }
    .form-box {
      width: 400px;
      height: 275px;
      // background-color: #cc3333;
    }
    .form-phone {
      padding: 0 20px;
    }
  }
}
</style>
